/*
 * Created on Oct 15, 2003
 *
 */
package schoolsource.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Contains information for attachments.
 */
public class AttachmentContainer extends Container {

	// The map of timer buckets for all databases
	protected static Hashtable dbStores = new Hashtable(); 
	// The auto-generated id of the attachment
	private IntegerItem assignedAttachmentID =
		new IntegerItem(SchoolDBSQL.ATTASSIGNEDID, "", true);
	// The name of the file
	private StringItem fileName =
		new StringItem(SchoolDBSQL.ATTFILENAME, "", true);
	// The path to the file
	private StringItem filePath =
		new StringItem(SchoolDBSQL.ATTFILEPATH, "", true);
	// The size of the file
	private IntegerItem fileSize =
		new IntegerItem(SchoolDBSQL.ATTFILESIZE, "0", true);

	/* (non-Javadoc)
	 * @see schoolsource.model.Container#getValidationItems()
	 */
	public Item[] getValidationItems() {
		ArrayList al = new ArrayList();

		al.add(assignedAttachmentID);
		al.add(fileName);
		al.add(filePath);
		al.add(fileSize);

		return (Item[]) al.toArray(new Item[al.size()]);
	}

	/**
	 * Default constructor
	 *
	 */
	private AttachmentContainer() {

	}

	/**
	 * Constructor that loads information from the database based on the ID passed in.
	 * @param aID A natural number corresponding to an auto-generated attachment ID in the database.
	 */
	 // TODO find all instances of 'SchoolDBSQL.DEVDATABASENAME' and make dynamic
	private AttachmentContainer(String aID, String dbName) {
		getAssignedAttachmentID().setValue(aID);
		//        assignedAttachmentID = new IntegerItem(SchoolDBSQL.ATTASSIGNEDID, aID, true);
		SchoolDBSQL.getSchoolDBSQL(dbName).loadAttachment(
			getValidationItems(),
			new Item[] { assignedAttachmentID });
	}

	public static AttachmentContainer getAttachmentContainer(String aID, String dbName) {
		AttachmentContainer returnVal = null;
		DBStore dbs = null;
		
		if (aID == "-1") {
			returnVal = new AttachmentContainer();
		}
		else {
			if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
				dbs = new DBStore();
				dbStores.put(dbName, dbs);
				dbs.start();
			}
			if ((returnVal = (AttachmentContainer) dbs.getContainer(aID)) == null) {
				returnVal = new AttachmentContainer();
				returnVal.getAssignedAttachmentID().setValue(aID);
				SchoolDBSQL.getSchoolDBSQL(dbName).loadAttachment(returnVal.getValidationItems(), new Item[] {returnVal.getAssignedAttachmentID()});
				dbs.addContainer(returnVal, aID);
			}
		}

		return returnVal;
	}

	/**
	 * Getter for the assigned attachment ID.
	 * @return The auto-generated ID of the attachment.
	 */
	public IntegerItem getAssignedAttachmentID() {
		return assignedAttachmentID;
	}

	/**
	 * Getter for the file name
	 * @return The name of the attachment.
	 */
	public StringItem getFileName() {
		return fileName;
	}

	/**
	 * Getter for the file path.
	 * @return A string representation of the path where the attachment is stored
	 * relative to doc.
	 */
	public StringItem getFilePath() {
		return filePath;
	}

	/**
	 * Getter for the file size.
	 * @return A string representation of the file size, in bytes.
	 */
	public IntegerItem getFileSize() {
		return fileSize;
	}

	/**
	 * Prints the values of the container.
	 */
	public void printItems() {
		SchoolLogger.logger.log(
			Level.INFO,
			"************************************************");
		Item iList[] = getValidationItems();
		for (int j = 0; j < iList.length; j++) {
			if (iList[j] != null) {
				SchoolLogger.logger.log(
					Level.INFO,
					iList[j].getFieldName() + ": " + iList[j].toString());
			}
		}
	}
}
